package com.wushi.smartorm;

import org.apache.commons.lang3.tuple.Pair;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * sql的执行器
 * TODO 可以在执行sql中增加Intercepter Chain
 *
 * @program: smartorm
 * @description:
 * @author: Grant Wu
 * @create: 2020-05-30 11:26
 **/

public class OrmExecutor {
    public Map<String, Object> proccess(Pair<String, String> sqlWrapper) {
        String key = sqlWrapper.getKey();
        String sql = sqlWrapper.getValue();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet;
        try {
            connection = JDBCUtils.getConnection();
            preparedStatement = connection.prepareStatement(sql);

            switch (key) {
                case MapperConfig.SELECT_SQL:
                    resultSet = preparedStatement.executeQuery();
                    Map<String, Object> datas = parseResultSet(resultSet);
                    resultSet.close();
                    return datas;
//                case MapperConfig.UPDATE_SQL:
//                    int i = preparedStatement.executeUpdate();
//                    return i;
                default:
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 解析结果到Map： key-columnName， value-value
     * 注意metaData的index是从1开始的！！！
     *
     * @param resultSet
     * @return
     * @throws SQLException
     */
    private Map<String, Object> parseResultSet(ResultSet resultSet) throws SQLException {
        //获取table的metaData
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        Map<String, Object> datas = new HashMap<>();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                datas.put(metaData.getColumnName(i), getColumnValue(resultSet, metaData, i));
            }
        }
        return datas;
    }

    /**
     * 注意这里Siwtch的用法：多个case如果处理逻辑相同可以共用一段逻辑代码； 如果case后面直接return结果，则可以不需要break
     *
     * @param resultSet
     * @param metaData
     * @param index
     * @return
     * @throws SQLException
     */
    private Object getColumnValue(ResultSet resultSet, ResultSetMetaData metaData, int index) throws SQLException {
        String mysqlDataType = metaData.getColumnTypeName(index);
        switch (mysqlDataType) {
            case MapperConfig.TINYINT:
            case MapperConfig.SMALLINT:
            case MapperConfig.INT:
                return resultSet.getInt(index);
            case MapperConfig.BIGINT:
                return resultSet.getLong(index);
            case MapperConfig.FLOAT:
            case MapperConfig.DOUBLE:
                return resultSet.getFloat(index);
            case MapperConfig.VARCHAR:
                return resultSet.getString(index);
            case MapperConfig.DATETIME:
                return resultSet.getDate(index);
            default:
                return null;
        }

    }
}
